#include "gamehost.h"

void main(int argc, char **argv)
{

        int			i, maxi, maxfd, listenfd, connfd, sockfd;
	int			contfd;
	int			nready, client[MAXCLIENTS];
	int			playerAvailable;
	int			*ships;
	ssize_t			n;
	fd_set			rset, allset;
	char			buf[MAXLINE];
	socklen_t		clilen;
	struct sockaddr_in	cliaddr, servaddr, contaddr;

	playerfd = -1;
	playernumber = -1;
	playerAvailable = 0;

	listenfd = socket(AF_INET, SOCK_STREAM, 0);

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family      = AF_INET;
	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	servaddr.sin_port        = htons(SERV_PORT);

	bind(listenfd, (SA *) &servaddr, sizeof(servaddr));

	listen(listenfd, LISTENQ);

	maxfd = listenfd;			/* initialize */
	maxi = -1;				/* index into client[] array */
	for (i = 0; i < FD_SETSIZE; i++)
		client[i] = -1;			/* -1 indicates available entry */
	FD_ZERO(&allset);
	FD_SET(listenfd, &allset);

/*connect to the controller*/

	contfd = socket(AF_INET, SOCK_STREAM, 0);

	bzero(&contaddr, sizeof(contaddr));
	contaddr.sin_family = AF_INET;
	contaddr.sin_port = htons(SERV_PORT);
	inet_pton(AF_INET, argv[1], &contaddr.sin_addr);

	connect(contfd, (SA *) &contaddr, sizeof(contaddr));


        getSpecs();
}

int getSpecs(char **specs, struct sockaddr *cliaddr, int *ships, int shipslen, int makecon)
{
        int ptr = 0;
        int addrlen = 0;

        if(inet_ntop(AF_INET, (void*) cliaddr, *specs + 1, MSGSIZE-3) == 0){
            perror("inet_ntop");
            return -1;
        }
        //TODO pitää parsettaa..
        ptr = strlen(*specs+1);
        addrlen = ptr;
        *specs[0] = addrlen;
        ptr++;
	memcpy(*specs + ptr, ships, shipslen);
	ptr += shipslen;
	*specs[ptr] = makecon;

        printf("%s\n", *specs);
        
	return 1;
}

